Imports Ocean.OceanFramework.Validation.StringValidationRules

Namespace Validation
    <AttributeUsage(AttributeTargets.Property, AllowMultiple:=False, Inherited:=False)> Public NotInheritable Class RegularExpressionValidatorAttribute
        Inherits BaseValidatorAttribute

#Region " Declarations "

        Private _enumRegularExpressionPatternType As RegularExpressionPatternType = RegularExpressionPatternType.Custom
        Private _enumRequiredEntry As RequiredEntry = RequiredEntry.No
        Private _strCustomRegularExpressionPattern As String = String.Empty

#End Region

#Region " Properties "

        Public ReadOnly Property CustomRegularExpressionPattern() As String
            Get
                Return _strCustomRegularExpressionPattern
            End Get
        End Property

        Public ReadOnly Property RegularExpressionPatternType() As RegularExpressionPatternType
            Get
                Return _enumRegularExpressionPatternType
            End Get
        End Property

        Public ReadOnly Property RequiredEntry() As RequiredEntry
            Get
                Return _enumRequiredEntry
            End Get
        End Property

#End Region

#Region " Constructor "

        Public Sub New(ByVal enumRegularExpressionPatternType As RegularExpressionPatternType, ByVal enumRequiredEntry As RequiredEntry)
            _enumRegularExpressionPatternType = enumRegularExpressionPatternType
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal strCustomRegularExpressionPattern As String, ByVal enumRequiredEntry As RequiredEntry)
            _enumRegularExpressionPatternType = Validation.RegularExpressionPatternType.Custom
            _enumRequiredEntry = enumRequiredEntry

            If Not IsRegularExpressionPatternValid(strCustomRegularExpressionPattern) Then
                Throw New ArgumentOutOfRangeException("strCustomRegularExpressionPattern", "Programmer did not supply a valid CustomRegularExpressionPattern")
            End If

            _strCustomRegularExpressionPattern = strCustomRegularExpressionPattern
        End Sub

#End Region

#Region " Methods "

        Public Overrides Function Create(ByVal strPropertyName As String) As Validator
            Return New Validator(AddressOf Validation.StringValidationRules.RegularExpressionRule, New RegulatExpressionRuleEventArgs(Me, strPropertyName), RuleType.Attribute)
        End Function

#End Region

    End Class

End Namespace
